package com.designpattern.prototypepattern;

/**
 * @author Liusr
 * @date 2023/3/10
 * @package com.designpattern.prototypepattern
 * @Description:
 */
public class PrototypePattern {
	public static void main(String[] args) {
		Resume a = new Resume("张三");
		a.setPersonalInfo("男","29");
		a.setWorkExperience("1980-2000","xx公司");

		//b和c都克隆语a，但当他们都设置了工作经历时，我们希望的结果是三个显示的不一样
		try {
			Resume b = (Resume) a.clone();
			b.setWorkExperience("1998-2006","yy公司");

			Resume c = (Resume) a.clone();
			c.setWorkExperience("1998-2003","zz公司");

			a.Display();
			b.Display();
			c.Display();
		} catch (CloneNotSupportedException e) {
			throw new RuntimeException(e);
		}
	}
}

//工作经历
class WorkExperience implements Cloneable{
	private String workDate;
	private String company;

	public String getWorkDate() {
		return workDate;
	}

	public void setWorkDate(String workDate) {
		this.workDate = workDate;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	@Override
	public String toString() {
		return "WorkExperience{" +
				"workDate='" + workDate + '\'' +
				", company='" + company + '\'' +
				'}';
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		Object clone = null;
		clone = super.clone();
		return clone;
	}
}

class Resume implements Cloneable{
	private String name;
	private String sex;
	private String age;
	private WorkExperience work;

	public Resume(String name){
		this.name = name;
		work = new WorkExperience();
	}

	private Resume(WorkExperience work){
		try {
			this.work = (WorkExperience) work.clone();
		} catch (CloneNotSupportedException e) {
			throw new RuntimeException(e);
		}
	}
	//设置个人信息
	public void setPersonalInfo(String sex, String age){
		this.sex = sex;
		this.age =age;
	}
	//设置工作经历
	public void setWorkExperience(String workDate, String company){
		work.setWorkDate(workDate);
		work.setCompany(company);
	}
	//显示
	public void Display(){
		System.out.println(name+","+sex+","+age);
		System.out.println("工作经历："+work.getWorkDate()+","+work.getCompany());
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		Resume resume = new Resume(this.work);
		resume.name = this.name;
		resume.sex = this.sex;
		resume.age = this.age;
		return resume;
	}
}


